package cn.com.bo.util.http;

import java.security.NoSuchAlgorithmException;

import javax.annotation.PostConstruct;
import javax.net.ssl.SSLContext;

import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
/**
 * 连接池
 * @author Zhang Shaobo
 *
 */
public class HttpConnectionManager { 

//	private static HttpParams httpParams;
//	private static ClientConnectionManager connectionManager;

	/**
	 * 最大连接数
	 */
	public final static int MAX_TOTAL_CONNECTIONS = 200;
	/**
	 * 获取连接的最大等待时间
	 */
	public final static int WAIT_TIMEOUT = 60000;
	/**
	 * 每个路由最大连接数
	 */
	public final static int MAX_ROUTE_CONNECTIONS = 20;
	/**
	 * 连接超时时间
	 */
	public final static int CONNECT_TIMEOUT = 100000;
	/**
	 * 读取超时时间
	 */
	public final static int READ_TIMEOUT = 100000;

//	static {
//		httpParams = new BasicHttpParams();
//		// 设置最大连接数
//		ConnManagerParams.setMaxTotalConnections(httpParams, MAX_TOTAL_CONNECTIONS);
//		// 设置获取连接的最大等待时间
////		ConnManagerParams.setTimeout(httpParams, WAIT_TIMEOUT);
//		// 设置每个路由最大连接数
//		ConnPerRouteBean connPerRoute = new ConnPerRouteBean(MAX_ROUTE_CONNECTIONS);
//		ConnManagerParams.setMaxConnectionsPerRoute(httpParams,connPerRoute);
//		// 设置连接超时时间
//		HttpConnectionParams.setConnectionTimeout(httpParams, CONNECT_TIMEOUT);
//		// 设置读取超时时间
//		HttpConnectionParams.setSoTimeout(httpParams, READ_TIMEOUT);
//
//		SchemeRegistry registry = new SchemeRegistry();
//		registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
//		registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
//
//		connectionManager = new ThreadSafeClientConnManager(httpParams, registry);
//	}
//	public static HttpClient getDefaultHttpClient() {
//		return new DefaultHttpClient(connectionManager, httpParams);
//	}
	
	
	public static PoolingHttpClientConnectionManager cm = null;
    
    @PostConstruct
    public void init() {
        LayeredConnectionSocketFactory sslsf = null;
        try {
            sslsf = new SSLConnectionSocketFactory(SSLContext.getDefault());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
                .register("https", sslsf)
                .register("http", new PlainConnectionSocketFactory())
                .build();
        cm =new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        cm.setMaxTotal(MAX_TOTAL_CONNECTIONS);
        cm.setDefaultMaxPerRoute(MAX_ROUTE_CONNECTIONS);
    }

    public static CloseableHttpClient getDefaultHttpClient() {       
        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(cm)
                .build();          
        
        /*CloseableHttpClient httpClient = HttpClients.createDefault();//如果不采用连接池就是这种方式获取连接*/
        return httpClient;
        
    }

}